package org.edu.modules.skill.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.skill.entity.VeJwJingsaiBaoming;
import org.edu.modules.skill.entity.VeJwJingsaiGroup;
import org.edu.modules.skill.service.IVeJwJingsaiBaomingService;
import org.edu.modules.skill.service.IVeJwJingsaiGroupService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


 /**
 * @Description: 竞赛团队
 * @Author: jeecg-boot
 * @Date:   2021-04-15
 * @Version: V1.0
 */
@Api(tags="竞赛团队")
@RestController
@RequestMapping("/jingsaiProject/veJwJingsaiGroup")
@Slf4j
public class VeJwJingsaiGroupController extends BaseController<VeJwJingsaiGroup, IVeJwJingsaiGroupService> {
	@Autowired
	private IVeJwJingsaiGroupService veJwJingsaiGroupService;

	 @Autowired
	 private IVeJwJingsaiBaomingService veJwJingsaiBaomingService;

	 /**
	 * 分页列表查询
	 *
	 * @param veJwJingsaiGroup
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "竞赛团队-分页列表查询")
	@ApiOperation(value="竞赛团队-分页列表查询", notes="竞赛团队-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(VeJwJingsaiGroup veJwJingsaiGroup,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwJingsaiGroup> queryWrapper = QueryGenerator.initQueryWrapper(veJwJingsaiGroup, req.getParameterMap());
		Page<VeJwJingsaiGroup> page = new Page<VeJwJingsaiGroup>(pageNo, pageSize);
		IPage<VeJwJingsaiGroup> pageList = veJwJingsaiGroupService.selectTime(page, queryWrapper);

		pageList.getRecords().forEach(item->{
			LambdaQueryWrapper<VeJwJingsaiBaoming> baomingLambdaQueryWrapper = new LambdaQueryWrapper<>();
			baomingLambdaQueryWrapper.eq(VeJwJingsaiBaoming::getGroupid,item.getId());
			List<VeJwJingsaiBaoming> baomingList =veJwJingsaiBaomingService.list(baomingLambdaQueryWrapper);
			String teamUserIds = "";
			for (VeJwJingsaiBaoming it:baomingList){
				teamUserIds+=teamUserIds.length() == 0? it.getStuid():","+it.getStuid();
			}
			item.setTeamStudentIds(teamUserIds);
		});
//		System.out.println(pageList.getRecords());
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param veJwJingsaiGroup
	 * @return
	 */
	@AutoLog(value = "竞赛团队-添加")
	@ApiOperation(value="竞赛团队-添加", notes="竞赛团队-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwJingsaiGroup veJwJingsaiGroup) {
		veJwJingsaiGroup.setCreatetime(Integer.valueOf(System.currentTimeMillis()/1000 + ""));
		veJwJingsaiGroupService.save(veJwJingsaiGroup);
		LambdaQueryWrapper<VeJwJingsaiBaoming> baomingLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baomingLambdaQueryWrapper.eq(VeJwJingsaiBaoming::getGroupid,veJwJingsaiGroup.getId());
		veJwJingsaiBaomingService.remove(baomingLambdaQueryWrapper);
		for(String id:veJwJingsaiGroup.getTeamStudentIds().split(",")){
			VeJwJingsaiBaoming item=new VeJwJingsaiBaoming();
			item.setGroupid(veJwJingsaiGroup.getId());
			item.setProjectid(veJwJingsaiGroup.getProjectid());
			item.setStuid(Integer.valueOf(id));
			veJwJingsaiBaomingService.save(item);
		}
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param veJwJingsaiGroup
	 * @return
	 */
	@AutoLog(value = "竞赛团队-编辑")
	@ApiOperation(value="竞赛团队-编辑", notes="竞赛团队-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwJingsaiGroup veJwJingsaiGroup) {
		veJwJingsaiGroupService.updateById(veJwJingsaiGroup);
		LambdaQueryWrapper<VeJwJingsaiBaoming> baomingLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baomingLambdaQueryWrapper.eq(VeJwJingsaiBaoming::getGroupid,veJwJingsaiGroup.getId());
		veJwJingsaiBaomingService.remove(baomingLambdaQueryWrapper);
		for(String id:veJwJingsaiGroup.getTeamStudentIds().split(",")){
			VeJwJingsaiBaoming item=new VeJwJingsaiBaoming();
			item.setGroupid(veJwJingsaiGroup.getId());
			item.setProjectid(veJwJingsaiGroup.getProjectid());
			item.setStuid(Integer.valueOf(id));
			veJwJingsaiBaomingService.save(item);
		}
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "竞赛团队-通过id删除")
	@ApiOperation(value="竞赛团队-通过id删除", notes="竞赛团队-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		veJwJingsaiGroupService.removeById(id);
		LambdaQueryWrapper<VeJwJingsaiBaoming> baomingLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baomingLambdaQueryWrapper.eq(VeJwJingsaiBaoming::getGroupid,id);
		veJwJingsaiBaomingService.remove(baomingLambdaQueryWrapper);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "竞赛团队-批量删除")
	@ApiOperation(value="竞赛团队-批量删除", notes="竞赛团队-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.veJwJingsaiGroupService.removeByIds(Arrays.asList(ids.split(",")));
		LambdaQueryWrapper<VeJwJingsaiBaoming> baomingLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baomingLambdaQueryWrapper.in(VeJwJingsaiBaoming::getGroupid,Arrays.asList(ids.split(",")));
		veJwJingsaiBaomingService.remove(baomingLambdaQueryWrapper);
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "竞赛团队-通过id查询")
	@ApiOperation(value="竞赛团队-通过id查询", notes="竞赛团队-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwJingsaiGroup veJwJingsaiGroup = veJwJingsaiGroupService.getById(id);
		if(veJwJingsaiGroup==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(veJwJingsaiGroup);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param veJwJingsaiGroup
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwJingsaiGroup veJwJingsaiGroup) {
        return super.exportXls(request, veJwJingsaiGroup, VeJwJingsaiGroup.class, "竞赛团队");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwJingsaiGroup.class);
    }




}
